package com.aqie.arithmetic.datastructure;

import java.util.Iterator;

/**
 * 基于链表实现 背包 FILO
 */
public class LinkedListBag<T> implements Iterable<T> {
    // 链表的首节点
    private Node first;
    private class Node{
        T item;
        Node next;
    }

    // 和Stack 的 push完全相同
    public void add(T item){
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
    }

    @Override
    public Iterator<T> iterator() {
        return new ListIterator();
    }

    private class ListIterator implements Iterator<T>{
        private Node current = first;
        public boolean hasNext(){
            return current != null;
        }
        public void remove(){}

        public T next(){
            T item = current.item;
            current = current.next;
            return item;
        }

    }
}
